package com.fengye.security.security.repository;

import com.fengye.security.domain.Role;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.stereotype.Repository;

import java.util.Optional;
import java.util.Set;

/**
 * @author fengyexjtu@126.com
 * @date 2022年05月24日 2:50 PM
 */
@Repository
public interface RoleRepository extends JpaRepository<Role, Long>, QuerydslPredicateExecutor<Role> {

    Optional<Role> findOptionalByRoleName(String roleName);

    Set<Role> findByIdIn(Set<Long> ids);

    @Query("select count(r) from Role r inner join r.users ru where r.id = ?1")
    long countByAssigned(Long id);

    long countByRoleNameIgnoreCase(String roleName);

    long countByRoleNameIgnoreCaseAndIdNot(String roleName, Long Id);

    // @Override
    // default void customize(QuerydslBindings bindings, QRole root) {
    //     bindings.bind(root.roleName).first(StringExpression::containsIgnoreCase);
    //     bindings.bind(root.displayName).first(StringExpression::containsIgnoreCase);
    // }
}